/**
 * yeahmobi.com Inc.
 * Copyright © 2014 YeahMobi | All Rights Reserved.
 */   
    
package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.FbUserBusinessRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.FbUserBusinessManager;
import com.ndp.fb.redis.api.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.PreparedStatementCreator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Title: FbUserBusinessRdbServiceImpl.java
 * @Copyright: Copyright © 2015
 * @Description: 
 * @Company: yeahmobi
 * @Created on 上午10:43:41
 * @author giser.yang
 */
@Service
public class FbUserBusinessRdbServiceImpl extends DaoTemplate implements FbUserBusinessRdbService {

    @Autowired
    private RedisClient redisClient;
	/** 
	 * @discription
	 * @author giser.yang       
	 * 创建时间 2015年6月11日 上午10:43:41      
	 * @param fbUserId
	 * @param fbBusinessId
	 * @return     
	 * @see com.ndp.fb.rdb.api.FbUserBusinessRdbService#getFbUserBusinessManager(java.lang.Long, java.lang.Long)     
	 */

	@Override
	public FbUserBusinessManager getFbUserBusinessManager(Long fbUserId,
			Long fbBusinessId) {
		String sql = "SELECT * FROM `fb_user_business_manager` WHERE fb_user_id = ? and fb_business_id = ?";
		return findOne(sql, new Object[]{fbUserId,fbBusinessId}, DefaultRowMapper.autoRowMapper(FbUserBusinessManager.class));
	}

	/** 
	 * @discription
	 * @author giser.yang       
	 * 创建时间 2015年6月11日 上午10:43:41      
	 * @param fbUserId
	 * @return     
	 * @see com.ndp.fb.rdb.api.FbUserBusinessRdbService#getFbUserBusinessManagersByFbUserId(java.lang.Long)     
	 */

	@Override
	public List<FbUserBusinessManager> getFbUserBusinessManagersByFbUserId(
			Long fbUserId) {
		String sql = "SELECT * FROM `fb_user_business_manager` WHERE fb_user_id = ?";
		return getJdbcTemplate().query(sql, new Object[]{fbUserId},DefaultRowMapper.autoRowMapper(FbUserBusinessManager.class));
	}

	/** 
	 * @discription
	 * @author giser.yang       
	 * 创建时间 2015年6月11日 上午10:43:41      
	 * @param fbBusinessId
	 * @return     
	 * @see com.ndp.fb.rdb.api.FbUserBusinessRdbService#getFbUserBusinessManagersByFbBusinessId(java.lang.Long)     
	 */

	@Override
	public List<FbUserBusinessManager> getFbUserBusinessManagersByFbBusinessId(
			Long fbBusinessId) {
		String sql = "SELECT * FROM `fb_user_business_manager` WHERE fb_business_id = ?";
		return getJdbcTemplate().query(sql, new Object[]{fbBusinessId},DefaultRowMapper.autoRowMapper(FbUserBusinessManager.class));
	}

	/** 
	 * @discription 已经增加了redis乐观锁不会重复入库
	 * @author giser.yang       
	 * 创建时间 2015年6月11日 上午10:50:11      
	 * @param fbUserBusinessManager
	 * @return     
	 * @see com.ndp.fb.rdb.api.FbUserBusinessRdbService#saveFbUserBusinessManager(com.ndp.fb.rdb.model.FbUserBusinessManager)     
	 */  
	    
	@Override
	public Long saveFbUserBusinessManager(
			final FbUserBusinessManager fbUserBusinessManager) {
		String key = "FBUSERBUSINESSMANAGER"+fbUserBusinessManager.getFbUserId()+fbUserBusinessManager.getFbBusinessId();
    	if(!redisClient.setNx(key, fbUserBusinessManager)){
    		return 0L;
    	}
		try {
			redisClient.expire(key, 5, TimeUnit.SECONDS.name());
	    	FbUserBusinessManager fbUserBusinessManager1 = this.getFbUserBusinessManager(fbUserBusinessManager.getFbUserId(), fbUserBusinessManager.getFbBusinessId());
	    	if(fbUserBusinessManager1!=null){
	    		this.updateFbUserBusinessManager(fbUserBusinessManager);
	    		return fbUserBusinessManager1.getId();
	    	}
			return saveResultId(fbUserBusinessManager);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}finally{
			redisClient.delete(key);
		}
		return 0L;
	}

	/** 
	 * @discription
	 * @author giser.yang       
	 * 创建时间 2015年6月11日 上午10:55:01      
	 * @param fbUserId
	 * @param fbBusinessId
	 * @return     
	 * @see com.ndp.fb.rdb.api.FbUserBusinessRdbService#deleteFbUserBusinessManager(java.lang.Long, java.lang.Long)     
	 */  
	    
	@Override
	public Integer deleteFbUserBusinessManager(final Long fbUserId, final Long fbBusinessId) {
		String sql = "delete from fb_user_business_manager WHERE fb_user_id = ? and fb_business_id = ?";
		getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setObject(1, fbUserId);
				ps.setObject(2, fbBusinessId);
            }

            @Override
            public int getBatchSize() {
                return 1;
            }
        });
        return 1;
	}

	@Override
	public Integer updateFbUserBusinessManager(final FbUserBusinessManager fbUserBusinessManager) {
		return getJdbcTemplate().update(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(Connection con)
					throws SQLException {
				String sql = "UPDATE fb_user_business_manager SET "
						+ "role = ?,status = ? where fb_user_id = ? and fb_business_id = ?";
				PreparedStatement ps = con.prepareStatement(sql);
				ps.setObject(1, fbUserBusinessManager.getRole());
				ps.setObject(2, fbUserBusinessManager.getStatus());
				ps.setObject(3, fbUserBusinessManager.getFbUserId());
				ps.setObject(4, fbUserBusinessManager.getFbBusinessId());
				return ps;
			}
		});
	}

	@Override
	public Integer deleteFbUserBusinessManager(final Long fbUserId) {
		String sql = "delete from fb_user_business_manager WHERE fb_user_id = ?";
		getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setObject(1, fbUserId);
			}
			@Override
			public int getBatchSize() {
				return 1;
			}
		});
		return 1;
	}
}
